{% extends "base.html" %}
{% load i18n %}
{% load display_tags %}
{% load authorization_tags %}

{% block content %}
    {{ block.super }}
<h3 id="id_heading">{% blocktrans with full_name=user.get_full_name %}User: {{ full_name }}{% endblocktrans %}</h3>
<div class="row">
    <div id="tests" class="col-md-8">
        <div class="panel panel-default">
            <div class="panel-heading tight">
                <div class="clearfix">
                    <h3 class="pull-left">{% trans "Default Information" %}</h3>
                    <div class="dropdown pull-right">
                        <div class="btn-group">
                            <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenu1"
                                aria-label="Actions" data-toggle="dropdown" aria-expanded="true">
                                <span class="fa-solid fa-bars"></span>
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu1">
                                {% if "auth.change_user"|has_configuration_permission:request %}
                                <li>
                                    <a class="" href="{% url 'edit_user' user.id %}">
                                        <i class="fa-solid fa-pen-to-square"></i>{% trans "Edit" %}</a>
                                </li>
                                {% endif %}
                                <li role="separator" class="divider"></li>
                                <li>
                                    <a href="{% url 'action_history' user|content_type user.id %}">
                                        <i class="fa-solid fa-clock-rotate-left"></i>{% trans "View History" %}</a>
                                </li>
                                {% if "auth.delete_user"|has_configuration_permission:request and user.id != request.user.id %}
                                <li role="separator" class="divider"></li>
                                <li>
                                    <a class="" href="{% url 'delete_user' user.id %}" id="deleteUser"> 
                                        <i class="fa-solid fa-trash"></i>{% trans "Delete" %}</a>
                                </li>
                                {% endif %}
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table table-striped">
                    <tr>
                        <td><strong>{% trans "First Name" %}</strong></td>
                        <td>{{ user.first_name }}</td>
                    </tr>
                    <tr>
                        <td><strong>{% trans "Last Name" %}</strong></td>
                        <td>{{ user.last_name }}</td>
                    </tr>
                    <tr>
                        <td><strong>{% trans "Username" %}</strong></td>
                        <td>{{ user.username }}</td>
                    </tr>
                    <tr>
                        <td><strong>{% trans "Email" %}</strong></td>
                        <td>{{ user.email }}</td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <div class="clearfix">
                    <h4 class="pull-left">{% trans "Contact Information" %}</h4>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table table-striped">
                    <tr>
                        <td><strong>{% trans "Title" %}</strong></td>
                        <td>{% if user.usercontactinfo.title %} {{ user.usercontactinfo.title }} {% endif %}</td>
                    </tr>
                    <tr>
                        <td><strong>{% trans "Phone Number" %}</strong></td>
                        <td>{{ user.usercontactinfo.phone_number }}</td>
                    </tr>
                    <tr>
                        <td><strong>{% trans "Cell Number" %}</strong></td>
                        <td>{{ user.usercontactinfo.cell_number }}</td>
                    </tr>
                    <tr>
                        <td><strong>{% trans "Twitter Username" %}</strong></td>
                        <td>{% if user.usercontactinfo.twitter_username %} {{ user.usercontactinfo.twitter_username }}
                            {% endif %}</td>
                    </tr>
                    <tr>
                        <td><strong>{% trans "Github Username" %}</strong></td>
                        <td>{% if user.usercontactinfo.github_username %} {{ user.usercontactinfo.github_username }} {% endif %}</td>
                    </tr>
                    <tr>
                        <td><strong>{% trans "Slack Email Address" %}</strong></td>
                        <td>{% if user.usercontactinfo.slack_username %} {{ user.usercontactinfo.slack_username }} {%endif %}</td>

                    </tr>
                </table>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <div class="clearfix">
                    <h4 class="pull-left">{{ labels.ORG_USERS_LABEL }}</h4>
                    &nbsp;
                    <a href="https://docs.defectdojo.com/en/customize_dojo/user_management/about_perms_and_roles/#productproduct-type-membership--roles" target="_blank">
                        <i class="fa-solid fa-circle-question"></i></a>
                    {% if request.user.is_superuser %}
                    <div class="dropdown pull-right">
                        <button class="btn btn-primary dropdown-toggle" aria-label="Actions" type="button" id="dropdownMenuAddProductTypeMember"
                            data-toggle="dropdown" aria-expanded="true">
                            <span class="fa-solid fa-bars"></span>
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu2">
                            <li role="presentation">
                                <a id="addProductTypeMember" href="{% url 'add_product_type_member_user' user.id %}">
                                    <i class="fa-solid fa-plus"></i> {{ labels.ORG_USERS_ADD_ORGANIZATIONS_LABEL }}
                                </a>
                            </li>
                        </ul>
                    </div>
                    {% endif %}
                </div>
            </div>
            {% if product_type_members %}
            <div class="table-responsive">
                <table class="tablesorter-bootstrap table table-condensed table-striped">
                    <thead>
                        <tr>
                            <th></th>
                            <th>{{ labels.ORG_LABEL }}</th>
                            <th>{% trans "Role" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for member in product_type_members %}
                        <tr>
                            <td>
                                <ul>
                                    <li class="dropdown" style="list-style:none;position:absolute">
                                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" name="dropdownManageProductTypeMember"
                                            aria-expanded="true">&nbsp;<b class="fa-solid fa-ellipsis-vertical"></b>&nbsp;</a>
                                        <ul class="dropdown-menu">
                                            {% if member.product_type|has_object_permission:"Product_Type_Manage_Members" %}
                                            <li>
                                                <a name="editProductTypeMember" href="{% url 'edit_product_type_member' member.id %}">
                                                    <i class="fa-solid fa-pen-to-square"></i> {% trans "Edit" %}</a>
                                            </li>
                                            {% endif %}
                                            {% if member|has_object_permission:"Product_Type_Member_Delete" %}
                                            <li>
                                                <a name="deleteProductTypeMember" href="{% url 'delete_product_type_member' member.id %}">
                                                    <i class="fa-solid fa-trash"></i> {% trans "Delete" %}</a>
                                            </li>
                                            {% endif %}
                                        </ul>
                                    </li>
                                </ul>
                            </td>
                            <td name="member_product_type"><a href="{% url 'view_product_type' member.product_type.id %}">{{ member.product_type }}</a></td>
                            <td name="member_product_type_role">{{ member.role }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="panel-body">
                <small class="text-muted"><em>{{ labels.ORG_USERS_NO_ACCESS_MESSAGE }}</em></small>
            </div>
            {% endif %}
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <div class="clearfix">
                    <h4 class="pull-left">{{ labels.ASSET_USERS_ACCESS_LABEL }}</h4>
                    &nbsp;
                    <a href="https://docs.defectdojo.com/en/customize_dojo/user_management/about_perms_and_roles/#productproduct-type-membership--roles" target="_blank">
                        <i class="fa-solid fa-circle-question"></i></a>
                    {% if request.user.is_superuser %}
                    <div class="dropdown pull-right">
                        <button class="btn btn-primary dropdown-toggle" aria-label="Actions" type="button"  id="dropdownMenuAddProductMember"
                            data-toggle="dropdown" aria-expanded="true">
                            <span class="fa-solid fa-bars"></span>
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu3">
                            <li role="presentation">
                                <a id="addProductMember" href="{% url 'add_product_member_user' user.id %}">
                                    <i class="fa-solid fa-plus"></i> {{ labels.ASSET_USERS_ADD_LABEL }}
                                </a>
                            </li>
                        </ul>
                    </div>
                    {% endif %}
                </div>
            </div>
            {% if product_members %}
            <div class="table-responsive">
                <table class="tablesorter-bootstrap table table-condensed table-striped">
                    <thead>
                        <tr>
                            <th></th>
                            <th>{{ labels.ASSET_LABEL }}</th>
                            <th>{% trans "Role" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for member in product_members %}
                        <tr>
                            <td>
                                <ul>
                                    <li class="dropdown" style="list-style:none;position:absolute">
                                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" name="dropdownManageProductMember"
                                            aria-expanded="true">&nbsp;<b class="fa-solid fa-ellipsis-vertical"></b>&nbsp;</a>
                                        <ul class="dropdown-menu">
                                            {% if member.product|has_object_permission:"Product_Manage_Members" %}
                                            <li>
                                                <a name="editProductMember" href="{% url 'edit_product_member' member.id %}">
                                                    <i class="fa-solid fa-pen-to-square"></i> {% trans "Edit" %}</a>
                                            </li>
                                            {% endif %}
                                            {% if member|has_object_permission:"Product_Member_Delete" %}
                                            <li>
                                                <a name="deleteProductMember" href="{% url 'delete_product_member' member.id %}">
                                                    <i class="fa-solid fa-trash"></i> {% trans "Delete" %}</a>
                                            </li>
                                            {% endif %}
                                        </ul>
                                    </li>
                                </ul>
                            </td>
                            <td name="member_product"><a href="{% url 'view_product' member.product.id %}">{{ member.product }}</a></td>
                            <td name="member_product_role">{{ member.role }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="panel-body">
                <small class="text-muted"><em>{{ labels.ASSET_USERS_NO_ACCESS_MESSAGE }}</em></small>
            </div>
            {% endif %}
        </div>

        <div class="panel panel-default">
            <div class="panel-heading">
                <div class="clearfix">
                    <h4 class="pull-left">{% trans "Groups this User is a member of" %}</h4>
                    &nbsp;
                    <a href="https://docs.defectdojo.com/en/customize_dojo/user_management/about_perms_and_roles/#group-memberships" target="_blank">
                        <i class="fa-solid fa-circle-question"></i></a>
                    {% if request.user.is_superuser %}
                    <div class="dropdown pull-right">
                        <button class="btn btn-primary dropdown-toggle" aria-label="Actions" type="button" id="dropdownMenuAddGroupMember"
                                data-toggle="dropdown" aria-expanded="true">
                            <span class="fa-solid fa-bars"></span>
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu4">
                            <li role="presentation">
                                <a id="addGroupMember" href="{% url 'add_group_member_user' user.id %}">
                                    <i class="fa-solid fa-plus"></i> {% trans "Add Groups" %}
                                </a>
                            </li>
                        </ul>
                    </div>
                    {% endif %}
                </div>
            </div>
            {% if group_members %}
            <div class="table-responsive">
                <table class="tablesorter-bootstrap table table-condensed table-striped">
                    <thead>
                    <tr>
                        <th></th>
                        <th>{% trans "Group" %}</th>
                        <th>{% trans "Role in this Group" %}</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for member in group_members %}
                    <tr>
                        <td>
                            <ul>
                                <li class="dropdown" style="list-style:none;position:absolute">
                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" name="dropdownManageProductTypeMember"
                                       aria-expanded="true">&nbsp;<b class="fa-solid fa-ellipsis-vertical"></b>&nbsp;</a>
                                    <ul class="dropdown-menu">
                                        {% if member.group|has_object_permission:"Group_Manage_Members" %}
                                        <li>
                                            <a name="editProductTypeMember" href="{% url 'edit_group_member' member.id %}">
                                                <i class="fa-solid fa-pen-to-square"></i> {% trans "Edit" %}</a>
                                        </li>
                                        {% endif %}
                                        {% if member|has_object_permission:"Group_Member_Delete" %}
                                        <li>
                                            <a name="deleteProductTypeMember" href="{% url 'delete_group_member' member.id %}">
                                                <i class="fa-solid fa-trash"></i> {% trans "Delete" %}</a>
                                        </li>
                                        {% endif %}
                                    </ul>
                                </li>
                            </ul>
                        </td>
                        <td name="group_name"><a href="{% url 'view_group' member.group.id %}">{{ member.group }}</a></td>
                        <td name="group_role">{{ member.role }}</td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="panel-body">
                <small class="text-muted"><em>{% trans "This User is not a member of any Groups." %}</em></small>
            </div>
            {% endif %}
        </div>
    </div>
    <div class="col-md-4">
        <div class="panel panel-default-secondary">
            <div class="panel-heading">
                <h3 class="panel-title"><span class="fa-solid fa-circle-info fa-fw" aria-hidden="true"></span>
                    {% trans "Metadata" %}
                </h3>
            </div>
            <div class="table-responsive">
                <table class="table table-striped">
                    <tbody>
                        <tr>
                            <td style="width: 200px;"><strong>{% trans "Active" %}</strong></td>
                            <td>
                                {% if user.is_active %}
                                <i class="text-success fa-solid fa-check"></i>
                                {% else %}
                                <i class="text-danger fa-solid fa-xmark"></i>
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 200px;"><strong>{% trans "Superuser" %}</strong></td>
                            <td>
                                {% if user.is_superuser %}
                                <i class="text-success fa-solid fa-check"></i>
                                {% else %}
                                <i class="text-danger fa-solid fa-xmark"></i>
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 200px;"><strong>{% trans "Block execution" %}</strong></td>
                            <td>
                                {% if user.usercontactinfo.block_execution %}
                                <i class="text-success fa-solid fa-check"></i>
                                {% else %}
                                <i class="text-danger fa-solid fa-xmark"></i>
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 200px;"><strong>{% trans "Global role" %}</strong></td>
                            <td>{% if user.global_role.role %} {{ user.global_role.role }} {% endif %}</td>
                        </tr>
                        <tr>
                            <td style="width: 200px;"><strong>{% trans "Date Joined" %}</strong></td>
                            <td>{{ user.date_joined }}</td>
                        </tr>
                        <tr>
                            <td style="width: 200px;"><strong>{% trans "Last Login" %}</strong></td>
                            <td>{% if user.last_login %} {{ user.last_login }} {% else %} {% trans "Never" %} {% endif %}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <!-- Configuration permissions -->
        {% if not user.is_superuser %}
        <div class="panel panel-default-secondary" id="edit_permissions">
            <div class="panel-heading">
                <h3 class="panel-title"><span class="fa-solid fa-lock-open-alt" aria-hidden="true"></span>&nbsp;&nbsp;{% trans "Configuration Permissions" %}</h3>
            </div>
            <form id="edit_permissions_form" class="form-horizontal" method="post" action="{% url 'edit_user_permissions' user.id %}">{% csrf_token %}
            <div class="table-responsive">
                <table class="table table-striped">
                    <tr>
                        <td id="saving_permissions">&nbsp;</td>
                        <td class="centered"><strong>{% trans "View" %}</strong></td>
                        <td class="centered"><strong>{% trans "Add" %}</strong></td>
                        <td class="centered"><strong>{% trans "Edit" %}</strong></td>
                        <td class="centered"><strong>{% trans "Delete" %}</strong></td>
                    </tr>
                    {% for field in configuration_permission_form.permission_fields %}
                    <tr>
                        <td><strong>{{ field.display_name }}</strong></td>
                        <td class="centered">
                            {% if field.view_codename %}
                            <input id="id_{{ field.view_codename }}" 
                                {% if not request.user.is_superuser %}disabled{% endif %}
                                name="{{ field.view_codename }}" 
                                aria-label="{{ field.view_codename }}" 
                                type="checkbox" 
                                {% if user|user_has_configuration_permission_without_group:field.view_codename %}checked{% endif %} />
                            {% endif %}
                        </td>
                        <td class="centered">
                            {% if field.add_codename %}
                            <input id="id_{{ field.add_codename }}"
                                {% if not request.user.is_superuser %}disabled{% endif %}
                                name="{{ field.add_codename }}"
                                aria-label="{{ field.add_codename }}"
                                type="checkbox"
                                {% if user|user_has_configuration_permission_without_group:field.add_codename %}checked{% endif %} />
                            {% endif %}
                        </td>
                        <td class="centered">
                            {% if field.change_codename %}
                            <input id="id_{{ field.change_codename }}"
                                {% if not request.user.is_superuser %}disabled{% endif %}
                                name="{{ field.change_codename }}"
                                aria-label="{{ field.change_codename }}"
                                type="checkbox"
                                {% if user|user_has_configuration_permission_without_group:field.change_codename %}checked{% endif %} />
                            {% endif %}
                        </td>
                        <td class="centered">
                            {% if field.delete_codename %}
                            <input id="id_{{ field.delete_codename }}"
                                {% if not request.user.is_superuser %}disabled{% endif %}
                                name="{{ field.delete_codename }}"
                                aria-label="{{ field.delete_codename }}"
                                type="checkbox"
                                {% if user|user_has_configuration_permission_without_group:field.delete_codename %}checked{% endif %} />
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </table>
            </div>
            </form>
        </div>
        {% endif %}
        <!-- End Configuration Permissions -->
    </div>
</div>
{% endblock %}
{% block postscript %}
{{ block.super }}
<script type="text/javascript">

    $(function() {
        $('div#edit_permissions input[type=checkbox]').on('click', function() {
            $('#saving_permissions').append(' <small>{% trans "(saving...)" %}</small>')
            $('#edit_permissions_form').submit()
        });
    });

</script>
{% endblock %}
